//
//  CalendarViewController.m
//  TodatPlay
//
//  Created by dllo on 16/9/12.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "CalendarViewController.h"
#import "SelfSizeTool.h"
#import "UIImageView+WebCache.h"
#import "CDMNetTool.h"
#import "TAMacro.h"
#import "SZCalendarPicker.h"
// model;
#import "CalendarData.h"
#import "CalendarRootClass.h"
#import "CalendarCollectionViewCell.h"
#import "CalendarCollectionReusableView.h"
#import "DetailsViewController.h"


#define kRootClasForRow self.rootClass.data[indexPath.row]
#define kCollectionViewCellReuse @"reuse"
#define kCollectionReuseableViewReuse @"headerReusableReuse"
@interface CalendarViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *calendarCollectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *calendatLayout;
@property (nonatomic, strong) CalendarRootClass *rootClass;
@end

@implementation CalendarViewController {
    NSInteger da;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // Do any additional setup after loading the view from its nib.
    [self.calendarCollectionView registerNib:[UINib nibWithNibName:@"CalendarCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kCollectionViewCellReuse];
    [self.calendarCollectionView registerClass:[CalendarCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionReuseableViewReuse];
    
    self.calendatLayout.minimumLineSpacing = 5;
    self.calendatLayout.minimumInteritemSpacing = 5;
    self.calendatLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    self.calendarCollectionView.bounces = NO;
    // 获取当前年月日
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay ;
       NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    NSInteger year = [dateComponent year];
    NSInteger month = [dateComponent month];
    NSInteger day = [dateComponent day];
    [self handleData:day andMonth:month andYear:year];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"left_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]  style:UIBarButtonItemStylePlain target:self action:@selector(handleLeft:)];
    
}
#pragma mark - 实现左按钮的点击方法
- (void)handleLeft:(UIBarButtonItem *)barBtn {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 数据处理
- (void)handleData:(NSInteger)day andMonth:(NSInteger)month andYear:(NSInteger)year{
    NSString *URLString = @"http://wanapi.damai.cn/projlistnew.json?platform=4&cateid=0&source=10345&psize=20&version=203004&channel_from=m360_market&pindex=0";
    NSString *stimeStr= [NSString stringWithFormat:@"&stime=%ld-%ld-%ld", year, month, day];
    
    NSString *etimeStr =  [NSString stringWithFormat:@"&etime=%ld-%ld-%ld", year, month, day+1];
    NSString *cityidStr = @"&cityid=110000";
    NSString *finalStr = [[[URLString stringByAppendingString:stimeStr]stringByAppendingString:etimeStr]stringByAppendingString:cityidStr];
//    NSLog(@"%@", finalStr);
    [CDMNetTool GET:finalStr andBody:nil andHeader:nil andResponse:CDMJSON andSuccessBlock:^(id result) {
        self.rootClass = [CalendarRootClass modelWithDic:result];
        [self.calendarCollectionView reloadData];
        //        NSLog(@"请求成功:%@", result);
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请求失败:%@", error);
    }];

    
}
#pragma mark - datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.rootClass.data.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CalendarCollectionViewCell *cell
    = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellReuse forIndexPath:indexPath];
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:kRootClasForRow.pic] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
    cell.titlelabel.text = kRootClasForRow.title;
    cell.timestrLabel.text = kRootClasForRow.timestr;
    cell.viewCountLabel.text = [NSString stringWithFormat:@"%ld", kRootClasForRow.viewcount];
    cell.venuenameLabel.text = kRootClasForRow.venuename;
    cell.pricestr.text = kRootClasForRow.pricestr;
    cell.categorynameLabel.text = kRootClasForRow.categoryname;
    cell.contentView.backgroundColor = [UIColor whiteColor];
    return cell;
}
#pragma mark - 自适应高度返回cell大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = [SelfSizeTool selfSizeHeightWithString:self.rootClass.data[indexPath.row].title width:SCREEN_WIDTH - 20 font:[UIFont systemFontOfSize:17]];
    return CGSizeMake(SCREEN_WIDTH - 10, width + 220 + 0.5);
}
#pragma mark - 返回自定义头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    CalendarCollectionReusableView *reuseableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionReuseableViewReuse forIndexPath:indexPath];
    // block传值,
    [reuseableView calendar:^(NSInteger day, NSInteger month, NSInteger year) {

        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0 , 100, 44)];
        titleLabel.backgroundColor = [UIColor clearColor];  //设置Label背景透明
        titleLabel.font = [UIFont boldSystemFontOfSize:15];  //设置文本字体与大小
        titleLabel.textColor = [UIColor whiteColor];  //设置文本颜色
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = [NSString stringWithFormat:@"%ld年%ld月", year, month];  //设置标题
        self.navigationItem.titleView = titleLabel;
        
    }];
    // 调用点击的block
    [reuseableView click:^(NSInteger day, NSInteger month, NSInteger year) {
        [self handleData:day andMonth:month andYear:year];
        [self.calendarCollectionView reloadData];
    }];
    
    return reuseableView;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(SCREEN_WIDTH, 250);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    DetailsViewController *vc = [[DetailsViewController alloc] init];
    UINavigationController *detaNavi = [[UINavigationController alloc] initWithRootViewController:vc];
    vc.parm = self.rootClass.data[indexPath.row].ID;

    detaNavi.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

    [self presentViewController:detaNavi animated:YES completion:nil];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
